package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dto.LoginDto;
import com.heima.user.mapper.ApUserMapper;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    /**
     * 登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        Map<String,Object> map = new HashMap<>();
        //第一步：判断有数据时
        if(!StringUtils.isBlank(dto.getPhone()) && !StringUtils.isBlank(dto.getPassword())){
            //根据手机号查
            String phone = dto.getPhone();
            ApUser apUser = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, phone));
            if(apUser == null){
                //返回用户数据不存在
                return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }
            String password = dto.getPassword();//用户输入的密码
            String salt = apUser.getSalt();
            String md5 = DigestUtils.md5DigestAsHex((password + salt).getBytes());

            /*
            加密出来结果不对
            String encode = MD5Utils.encodeWithSalt(password, salt);
            */
            //密码不正确
            if(!md5.equals(apUser.getPassword())){
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //重置数据
            apUser.setPassword("");
            apUser.setSalt("");
            //生成token
            String token = AppJwtUtil.getToken(apUser.getId().longValue());

            map.put("token",token);
            map.put("user",apUser);
            return ResponseResult.okResult(map);
        }else {
            //游客登陆
            map.put("token",AppJwtUtil.getToken(0L));
            return ResponseResult.okResult(map);
        }
    }

    /**
     * 注册功能
     *
     * @param apUser
     * @return
     */
    @Override
    public ResponseResult register(ApUser apUser) {
        //判断前端传来的数据
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //
        String[] split = UUID.randomUUID().toString().split("-");
        String salt = split[1];
        String password = apUser.getPassword();
        String s = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        apUser.setPassword(s);
        String phone = apUser.getPhone();
        //补全一部分信息
        apUser.setSalt(salt);
        apUser.setCreatedTime(new Date());
        apUser.setFlag(Short.valueOf("0"));
        //查询该电话号码是否注册
        if(getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone,phone))==null){
            boolean save = save(apUser);
            if(save){
                return ResponseResult.okResult(200,"注册成功");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }
}
